//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using LitwareHR.PrivateApp.Presenters.ConfigCusto.Process;
using System.Collections.Generic;

public partial class ConfigCusto_Process_Ruleset : BasePage, IRulesetView
{
    private RulesetPresenter _presenter;

    protected override void OnInitComplete(EventArgs e)
    {
        base.OnInitComplete(e);
        _presenter = new RulesetPresenter(this);
    }

    public List<WorkflowRule> Ruleset
    {
        get
        {
            List<WorkflowRule> list = ViewState["Rules"] as List<WorkflowRule>;
            if (list == null)
            {
                list = new List<WorkflowRule>();
                ViewState["Rules"] = list;
            }

            return list;
        }
        set
        {
            ViewState["Rules"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            _presenter.OnViewLoaded();
        }
    }

    #region RowDataBound

    protected void RulesetGridView_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            BindRow(e.Row);
        }
    }

    private void BindRow(GridViewRow row)
    {
        WorkflowRule rule = (WorkflowRule)row.DataItem;
        FillProperties(row);
        SelectProperty(rule, row);
        FillOperations(row);
        SelectOperation(rule, row);
        FillWorkflows(row);
        SelectWorkflow(rule, row);
        SelectValue(rule, row);        
    }

    private void FillProperties(GridViewRow row)
    {
        DropDownList list = row.FindControl("PropertyDropDown") as DropDownList;
        if (list != null)
        {
            list.DataSource = _presenter.GetEntityProperties(Helpers.TenantAlias, "position");
            list.DataBind();
        }
    }

    private void SelectProperty(WorkflowRule rule, GridViewRow row)
    {
        string property = rule.Property;
        if (property != null)
        {
            DropDownList list = row.FindControl("PropertyDropDown") as DropDownList;
            list.SelectedValue = property;
        }
    }

    private void FillOperations(GridViewRow row)
    {
        DropDownList list = row.FindControl("OperationDropDown") as DropDownList;
        if (list != null)
        {
            list.DataTextField = "Value";
            list.DataValueField = "Key";
            list.DataSource = _presenter.Operations;
            list.DataBind();
        }
    }

    private void SelectOperation(WorkflowRule rule, GridViewRow row)
    {
        int operation = rule.Operation;
        DropDownList list = row.FindControl("OperationDropDown") as DropDownList;
        list.SelectedValue = operation.ToString();
    }

    private void FillWorkflows(GridViewRow row)
    {
        DropDownList list = row.FindControl("WorkflowDropDown") as DropDownList;
        if (list != null)
        {
            list.DataTextField = "Value";
            list.DataValueField = "Key";
            list.DataSource = _presenter.Workflows;
            list.DataBind();
        }
    }

    private void SelectWorkflow(WorkflowRule rule, GridViewRow row)
    {
        Guid workflowId = rule.TargetWorkflowId;
        if (workflowId != null)
        {
            DropDownList list = row.FindControl("WorkflowDropDown") as DropDownList;
            list.SelectedValue = workflowId.ToString();
        }
    }

    private void SelectValue(WorkflowRule rule, GridViewRow row)
    {
        TextBox valueTextBox = row.FindControl("ValueTextBox") as TextBox;
        valueTextBox.Text = rule.Value;
    }

    #endregion

    public void ShowRuleset()
    {
        RulesetGridView.DataSource = this.Ruleset;
        RulesetGridView.DataBind();
        AddButton.Enabled = RulesetGridView.Rows.Count < Constants.MaxRulesetRows;
        AddButton.Text = (AddButton.Enabled) ? "Add Step" : "Add Step (10 max)";
    }

    protected void AddButton_Click(object sender, EventArgs e)
    {
        _presenter.OnAddRule();
    }

    public void UpdateRuleset()
    {
        Ruleset.Clear();
        foreach (GridViewRow row in RulesetGridView.Rows)
        {
            WorkflowRule rule = new WorkflowRule();
            TextBox valueTextBox = row.FindControl("ValueTextBox") as TextBox;
            rule.Value = valueTextBox.Text;
            DropDownList propertyList = row.FindControl("PropertyDropDown") as DropDownList;
            rule.Property = propertyList.SelectedValue;
            DropDownList operationList = row.FindControl("OperationDropDown") as DropDownList;
            rule.Operation = Convert.ToInt32(operationList.SelectedItem.Value);
            DropDownList workflowList = row.FindControl("WorkflowDropDown") as DropDownList;
            rule.TargetWorkflowId = new Guid(workflowList.SelectedItem.Value);
            Ruleset.Add(rule);
        }
    }

    protected void SaveButton_Click(object sender, EventArgs e)
    {
        _presenter.OnSaveWorkflow();
        Response.Redirect("Default.aspx");
    }

    protected void CancelButton_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }

    protected void RulesetGridView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int index = Convert.ToInt32(e.CommandArgument);
        UpdateRuleset();

        switch (e.CommandName)
        {
            case "deleteStep":
                _presenter.OnRemoveRule(index);
                break;
            case "moveUpStep":
                _presenter.OnMoveRuleUp(index);
                break;
            case "moveDownStep":
                _presenter.OnMoveRuleDown(index);
                break;

            default:
                break;
        }
    }

    protected void RulesetGridView_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string rowIndex = e.Row.RowIndex.ToString();
            ImageButton deleteButton = e.Row.FindControl("ImageButtonDelete") as ImageButton;
            if (deleteButton != null)
            {
                deleteButton.CommandArgument = rowIndex;
            }
            ImageButton moveUpButton = e.Row.FindControl("ImageButtonMoveUp") as ImageButton;
            if (moveUpButton != null)
            {
                moveUpButton.CommandArgument = rowIndex;
            }
            ImageButton moveDownButton = e.Row.FindControl("ImageButtonMoveDown") as ImageButton;
            if (moveDownButton != null)
            {
                moveDownButton.CommandArgument = rowIndex;
            }
        }
    }

    protected void PropertyDropDown_SelectedIndexChanged(object sender, EventArgs e)
    {
        UpdateRuleset();
    }

    protected void OperationDropDown_SelectedIndexChanged(object sender, EventArgs e)
    {
        UpdateRuleset();
    }

    protected void WorkflowDropDown_SelectedIndexChanged(object sender, EventArgs e)
    {
        UpdateRuleset();
    }
}
